package com.quanquan.api;

import com.alibaba.fastjson.JSONObject;
import com.quanquan.common.Response;
import com.quanquan.entity.StudentInfo;
import com.quanquan.service.StudentInfoService;
import com.quanquan.service.StudentLessonService;
import com.quanquan.utils.ExcelUtil;
import com.quanquan.utils.TencentCaptcha;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping(value = "/api/student")
public class StudentInfoContrller {

    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private StudentLessonService studentLessonService;

    /**
     * 验证手机号
     * @return
     */
    @RequestMapping(value = "/validateMobile", method = RequestMethod.GET)
    @ResponseBody
    public Response validateMobile (String mobile) {
        if (mobile == null) {
            return new Response().failure("手机号不能为空！");
        }
        StudentInfo studentInfo = studentInfoService.findByMobile(mobile);
        if (studentInfo == null) {
            return new Response().failure("手机号无法登陆进行选课！");
        }
        return new Response().success();
    }

    /**
     * 手机号登录
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Response login (HttpServletRequest request, String mobile, String ticket, String randstr) {
        if (mobile == null) {
            return new Response().failure("手机号不能为空！");
        }
        StudentInfo studentInfo = studentInfoService.findByMobile(mobile);
        if (studentInfo == null) {
            return new Response().failure("手机号无法登陆进行选课！");
        }

        if ("online".equals(studentInfo.getOnline())) {
            return new Response().failure("该手机号已经在选课，请勿重复登录！");
        }
        Boolean choose = Boolean.FALSE;
        if (studentLessonService.findByStudentId(studentInfo.getId()) != null) {
            choose = Boolean.TRUE;
        }
        Map<String, Object> data = new HashMap<>();
        String token = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
        data.put("token", token);
        data.put("student", studentInfo);
        data.put("choose", choose);
        studentInfo.setToken(token);
        studentInfo.setOnline("online");
        studentInfoService.save(studentInfo);
        return new Response().success(data);
    }

//    @RequestMapping(value = "/import", method = RequestMethod.GET)
//    @ResponseBody
//    public Response importStudent () throws Exception{
//
////        for(Integer k = 169; k <= 187; k++) {
////            String filePath = "E:\\self-work\\运康中学选课系统\\chuer\\" + k.toString() + ".xlsx";
////            // 构造 XSSFWorkbook 对象，strPath 传入文件路径
////            XSSFWorkbook xwb = new XSSFWorkbook(filePath);
////            // 读取第一章表格内容
////            XSSFSheet sheet = xwb.getSheetAt(0);
////            // 定义 row、cell
////            XSSFRow row = null;
////            String cell = null;
////            // 循环输出表格中的内容
////            for (int i = 2; i < sheet.getPhysicalNumberOfRows(); i++) {
////                StudentInfo studentInfo = new StudentInfo();
////                row = sheet.getRow(i);
////                for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
////                    // 通过 row.getCell(j).toString() 获取单元格内容，
////                    row.getCell(j).setCellType(HSSFCell.CELL_TYPE_STRING);
////                    cell = row.getCell(j).toString();
////                    if (j == 1) {
////                        studentInfo.setName(cell);
////                    }
////                    if (j == 2) {
////                        studentInfo.setClassName(cell);
////                    }
////                    if (j == 3) {
////                        studentInfo.setMobile(cell);
////                    }
////                }
////                studentInfo.setGrade("chuer");
////                studentInfoService.save(studentInfo);
////            }
////        }
//            String filePath = "E:\\self-work\\运康中学选课系统\\初一年级学生信息正确版(1).xlsx";
//            // 构造 XSSFWorkbook 对象，strPath 传入文件路径
//            XSSFWorkbook xwb = new XSSFWorkbook(filePath);
//            // 读取第一章表格内容
//            XSSFSheet sheet = xwb.getSheetAt(0);
//            // 定义 row、cell
//            XSSFRow row = null;
//            String cell = null;
//            // 循环输出表格中的内容
//            for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
//                StudentInfo studentInfo = new StudentInfo();
//                row = sheet.getRow(i);
//                for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
//                    // 通过 row.getCell(j).toString() 获取单元格内容，
//                    row.getCell(j).setCellType(HSSFCell.CELL_TYPE_STRING);
//                    cell = row.getCell(j).toString();
//                    if (j == 3) {
//                        studentInfo.setName(cell);
//                    }
//                    if (j == 2) {
//                        studentInfo.setClassName(cell);
//                    }
//                    if (j == 5) {
//                        studentInfo.setMobile(cell);
//                    }
//                }
//                studentInfo.setGrade("chuyi");
//                studentInfoService.save(studentInfo);
//            }
//        return new Response().success();
//    }

    /**
     * 是否在线
     * @return
     */
    @RequestMapping(value = "/online", method = RequestMethod.GET)
    @ResponseBody
    public Response online (Long studentId, boolean leave) {
        if (studentId == null) {
            return new Response().failure("id不能为空！");
        }
        StudentInfo studentInfo = studentInfoService.findById(studentId);

        if (studentInfo != null) {
            studentInfo.setOnline("offline");
//            studentInfo.setLastTime(System.currentTimeMillis());
            studentInfoService.save(studentInfo);
            return new Response().success();
        }
        return new Response().success();
    }

    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        for (Integer i = 169; i<= 207; i++) {
            sb.append("'");
            sb.append(i.toString());
            sb.append("班', ");
        }
        System.out.println(sb);
    }
}
